﻿/**************************************************************************
创建时间:	2020/5/15 11:48:49    
作	  者:	张存
邮 	  箱:	zhangcunliang@126.com

Copyright (c) zhcun.cn

描	述：
记	录：
***************************************************************************/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ZhCun.DBModel;
using ZhCun.Utils;
using ZhCun.Utils.Helpers;
using ZhCun.Win.Controls;
using ZhCun.Win.Extend;

namespace ZhCun.Win.MenuRole
{
    public partial class FrmMenu : FrmBase
    {
        public FrmMenu()
        {
            InitializeComponent();
        }

        MenuBLL BLLObj { get; } = new MenuBLL();

        /// <summary>
        /// 初始化指定控件的tree数据
        /// </summary>
        void InitTreeData(TreeViewEx tv, ContextMenuStrip cmNodeOperate)
        {
            var treeData = BLLObj.GetTreeData();

            tv.InitTreeViewData(treeData, NodeAddHandle);

            void NodeAddHandle(TreeNode node, Sys_Menu m)
            {
                if (m.IsMenu)
                {
                    node.ImageIndex = 1;
                    node.SelectedImageIndex = 1;
                }
                else
                {
                    node.ImageIndex = 0;
                    node.ContextMenuStrip = cmNodeOperate;
                }
            }
        }

        void RefreshTreeData()
        {
            InitTreeData(tvMenu, cmNodeOperate);
            _CurrMenuTree = null;
            dgvMenu.DataSource = null;
        }

        private void FrmMenu_Load(object sender, EventArgs e)
        {
            RefreshTreeData();
            tvMenu.AllowDropNode = true;
            tvMenu.DropNodeBefore += TvMenu_DropNodeBefore;
            //tvMenu.DropNodeAfter += TvMenu_DropNodeAfter;
            //tvMenu.NodeMouseClick += TvMenu_NodeMouseClick;
            tvMenu.AfterSelect += TvMenu_AfterSelect;
        }
        
        #region 菜单树操作

        /// <summary>
        /// 当前选中的菜单分类对象
        /// </summary>
        Sys_Menu _CurrMenuTree;

        private void TvMenu_AfterSelect(object sender, TreeViewEventArgs e)
        {
            var selMenu = e.Node.Tag as Sys_Menu;
            if (selMenu.IsMenu) return;
            _CurrMenuTree = selMenu;
            RefreshMenuData();
        }
        
        private bool TvMenu_DropNodeBefore(TreeNode moveNode, TreeNode targetNode)
        {
            var selMenu = targetNode.Tag as Sys_Menu;
            if (selMenu.IsMenu)
            {
                ShowMessage("不能拖动到菜单项");
                return false;
            }

            var m = moveNode.Tag as Sys_Menu;
            m.OrderNo = targetNode.Nodes.Count;
            m.PId = targetNode.Name;
            var r = BLLObj.EditTree(m);
            ShowMessage(r.msg);
            return !!r;
        }

        private void tsBtnAddTree_Click(object sender, EventArgs e)
        {
            if (ShowInput(Save, "增加顶级节点..."))
            {
                RefreshTreeData();
            }
            ApiResult Save(string name)
            {
                return
                BLLObj.AddTree(new Sys_Menu
                {
                    MenuCaption = name,
                    OrderNo = tvMenu.Nodes.Count
                });
            }
        }

        private void tsBtnTreeRefresh_Click(object sender, EventArgs e)
        {
            RefreshTreeData();
        }

        private void tsMenuTreeAddSub_Click(object sender, EventArgs e)
        {
            TreeNode selNode = tvMenu.SelectedNode;
            ShowInput(Save, $"增加子节点 {selNode.FullPath}");

            ApiResult Save(string name)
            {
                var model = new Sys_Menu
                {
                    Id = BLLObj.NewId(),
                    PId = selNode.Name,
                    MenuCaption = name,
                    OrderNo = tvMenu.Nodes.Count
                };
                var ret = BLLObj.AddTree(model);
                if (ret)
                {
                    RefreshTreeData();
                    tvMenu.SetSelectedNode(model.Id);
                }
                return ret;
            }
        }

        private void tsMenuTreeEdit_Click(object sender, EventArgs e)
        {
            TreeNode selNode = tvMenu.SelectedNode;
            if (selNode == null) return;
            string title = $"修改节点名称 {selNode.FullPath}";
            if (ShowInput(Save, title))
            {
                RefreshTreeData();
            }
            ApiResult Save(string name)
            {
                return
                BLLObj.EditTree(new Sys_Menu
                {
                    Id = selNode.Name,
                    MenuName = name
                });
            }
        }

        private void tsMenuTreeDel_Click(object sender, EventArgs e)
        {
            if (ShowQuestion("确实要移除当前节点吗?", "移除确认"))
            {
                TreeNode selNode = tvMenu.SelectedNode;
                var ret = BLLObj.DelTree(selNode.Tag as Sys_Menu);
                if (ret)
                {
                    RefreshTreeData();
                }
                ShowMessage(ret.msg);
            }
        }

        private void tsMenuTreeSetTop_Click(object sender, EventArgs e)
        {
            if (ShowQuestion("确实要设置为顶级节点吗?", "设置确认"))
            {
                var selModel = tvMenu.SelectedNode.Tag as Sys_Menu;
                selModel.PId = string.Empty;
                selModel.OrderNo = tvMenu.Nodes.Count;
                var ret = BLLObj.EditTree(selModel);
                //var ret = BLLObj.ChangeParentId(selNode.Tag as Sys_Menu, string.Empty);
                if (ret)
                {
                    RefreshTreeData();
                }
                ShowMessage(ret.msg);
            }
        }


        #endregion

        #region 菜单操作

        void RefreshMenuData()
        {
            var ret = BLLObj.GetMenus(_CurrMenuTree.Id);
            dgvMenu.DataSource = ret.data;
        }
        
        void AddShow(bool isCopyAdd)
        {
            if (_CurrMenuTree == null)
            {
                ShowMessage("请选择一个菜单分类");
                return;
            }

            Sys_Menu setModel;
            if (isCopyAdd)
            {
                setModel = dgvMenu.GetSelectedClassData<Sys_Menu>();
                if (setModel == null)
                {
                    ShowMessage("没有选中记录");
                    return;
                }
            }
            else
            {
                setModel = new Sys_Menu();
                setModel.PId = _CurrMenuTree.Id;
            }
            setModel.PCaption = _CurrMenuTree.MenuCaption;
            FrmMenuEdit frm = new FrmMenuEdit(dgvMenu.RowCount + 1);
            var arg = new EditFormArgs
            {
                ClearId = true,
                SaveHandle = (menu) => BLLObj.AddMenu(menu as Sys_Menu),
                SaveModel = setModel,
                SetModel = setModel
            };
            var ret = frm.ShowDialog(arg);
            if (ret)
            {
                ShowMessage(ret.msg);
                RefreshTreeData();
                tvMenu.SetSelectedNode(setModel.PId);
                //RefreshMenuData();
            }
        }

        private void tsBtnAdd_Click(object sender, EventArgs e)
        {
            AddShow(false);
        }

        private void tsBtnCopyAdd_Click(object sender, EventArgs e)
        {
            AddShow(true);
        }

        private void tsBtnEdit_Click(object sender, EventArgs e)
        {
            var selMenu = dgvMenu.GetSelectedClassData<Sys_Menu>();
            if (selMenu == null)
            {
                ShowMessage("没有选中记录");
                return;
            }
            var selNode = tvMenu.SelectedNode;
            selMenu.PCaption = selNode.FullPath;
            var frm = new FrmMenuEdit();
            frm.Text += " 修改";
            var arg = new EditFormArgs
            {
                SaveHandle = (menu) => BLLObj.EditMenu(menu as Sys_Menu),
                SaveModel = selMenu,
                SetModel = selMenu
            };
            var ret = frm.ShowDialog(arg);
            if (ret)
            {
                ShowMessage(ret.msg);
                RefreshMenuData();
                RefreshTreeData();
                tvMenu.SetSelectedNode(selMenu.PId);
            }
        }

        private void tsBtnDelete_Click(object sender, EventArgs e)
        {
            var selMenu = dgvMenu.GetSelectedClassData<Sys_Menu>();
            if (selMenu == null)
            {
                ShowMessage("没有选中记录");
                return;
            }
            if (!ShowQuestion("确实要删除当前选中记录吗？", "删除确认"))
            {
                return;
            }
            var ret = BLLObj.DelMenu(selMenu);
            if (ret)
            {
                RefreshMenuData();
                RefreshTreeData();
                tvMenu.SetSelectedNode(selMenu.PId);
            }
            ShowMessage(ret.msg);
        }

        private void tsBtnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        #endregion
    }
}